<template>
  <div class="nexbie-about-page">
    <!-- 返回按钮 -->
    <button class="back-button" @click="handleBack">
      <svg
        xmlns="http://www.w3.org/2000/svg"
        width="20"
        height="20"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round">
        <path d="M19 12H5M5 12l7 7 7-7" />
      </svg>
      <span>Back</span>
    </button>

    <div class="about-container">
      <!-- 标题区域 -->
      <header class="about-header">
        <h1 class="brand-slogan">From Spark to Storefront. Your Imagination, Made Real.</h1>
      </header>

      <!-- 主体内容区 -->
      <div class="about-content">
        <!-- 左侧目录 -->
        <aside class="about-sidebar">
          <nav class="toc">
            <ul>
              <li
                v-for="(item, index) in aboutList"
                :key="index"
                :class="{ active: activeIndex === index }"
                @click="scrollToSection(index)"
                class="toc-item">
                <span class="toc-number">{{ index + 1 }}.</span>
                <span class="toc-title">{{ item.title }}</span>
              </li>
            </ul>
          </nav>
        </aside>

        <!-- 右侧内容 -->
        <main class="about-main">
          <div class="sections-container" ref="sectionsContainer" @scroll="handleScroll">
            <section
              v-for="(item, index) in aboutList"
              :key="index"
              :ref="(el) => (sectionRefs[index] = el)"
              class="about-section">
              <h2 class="section-heading">
                <span class="heading-number">{{ index + 1 }}.</span>
                {{ item.title }}
              </h2>
              <div class="section-body" v-html="item.content"></div>
            </section>
          </div>
        </main>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, nextTick } from 'vue';
  import { useRouter } from 'vue-router';
  const router = useRouter();

  // 品牌介绍数据（完全使用原文案）
  const aboutList = ref([
    {
      title: 'Welcome to Nexbie',
      content: `<p>Welcome to Nexbie, the place where your creativity gets real. We saw a universe of incredible ideas trapped on screens, held back by the complexities of turning digital art into physical products. We knew there had to be a better way.</p>
              <p>Nexbie is a one-stop community built for the new wave of AIGC creators. We are a platform designed to empower every user to complete the entire journey from a creative spark to a commercial success. Whether you're a master of prompts or a visionary designer, our tools are built to bring your concepts to life.</p>`,
    },
    {
      title: 'What We Do',
      content: `
      <ul>
        <li><strong>Create:</strong> Use our powerful Nex Lab AI tools to generate stunning designs from text or images. This is your personal creation engine.</li>
        <li><strong>Launch:</strong> Turn your best work into a product with our unique "Nex" feature. It’s a micro-crowdfunding system where the community backs your design to make it a reality. If a design gets enough support, we produce it, and you earn it.</li>
        <li><strong>Compete & Win:</strong> Showcase your skills in the Nexbie Arcade, our creative contest with real cash prizes. It's your stage to get noticed and rewarded.</li>
      </ul>
    `,
    },
    {
      title: 'Our Belief',
      content: `<p>We believe that every creator deserves a straightforward path to monetization. Nexbie is our answer. It's more than a platform; it's a launchpad for your ambition.</p>
              <p style="font-weight: 500; color: #f76b1b; font-size: 16px; margin-top: 20px;">Join us and build the future of creativity.</p>`,
    },
  ]);

  // 状态管理
  const activeIndex = ref(0);
  const sectionsContainer = ref<HTMLDivElement | null>(null);
  const sectionRefs = ref<any>([]);
  const scrollTimeout = ref<number | null>(null);

  // 滚动到指定章节（确保点击精准响应）
  const scrollToSection = (index: number) => {
    const container = sectionsContainer.value;
    const section = sectionRefs.value[index];

    if (container && section) {
      activeIndex.value = index;

      // 精确计算滚动位置，添加20px顶部间距
      const containerRect = container.getBoundingClientRect();
      const sectionRect = section.getBoundingClientRect();
      const scrollPosition = sectionRect.top - containerRect.top + container.scrollTop - 20;

      // 原生平滑滚动，兼容性稳定
      container.scrollTo({
        top: scrollPosition,
        behavior: 'smooth',
      });
    }
  };

  // 滚动监听，自动高亮当前章节
  const handleScroll = () => {
    // 防抖处理，提升性能
    if (scrollTimeout.value) {
      window.clearTimeout(scrollTimeout.value);
    }

    scrollTimeout.value = window.setTimeout(() => {
      if (!sectionsContainer.value) return;

      const container = sectionsContainer.value;
      const scrollTop = container.scrollTop;

      // 遍历章节找到当前可视项
      for (let i = 0; i < sectionRefs.value.length; i++) {
        const section = sectionRefs.value[i];
        if (!section) continue;

        const sectionTop = section.offsetTop;
        const sectionHeight = section.offsetHeight;

        // 章节顶部在可视区域内（50px容差，优化用户体验）
        if (sectionTop <= scrollTop + 50 && sectionTop + sectionHeight > scrollTop) {
          activeIndex.value = i;
          break;
        }
      }
    }, 100);
  };

  // 返回按钮处理
  const handleBack = () => {
    // console.log('返回上一页');
    // 实际应用中使用路由返回
    router.back();
  };

  // 初始化页面
  onMounted(() => {
    nextTick(() => {
      handleScroll();
    });
  });
</script>

<style scoped>
  .nexbie-about-page {
    min-height: 100vh;
    background-color: #f9f9f9;
    padding: 20px;
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    position: relative;
  }

  /* 返回按钮 */
  .back-button {
    display: flex;
    align-items: center;
    gap: 8px;
    background: transparent;
    border: none;
    color: #f76b1b;
    font-size: 14px;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 6px;
    transition: all 0.2s ease;
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
  }

  .back-button:hover {
    background-color: rgba(247, 107, 27, 0.1);
  }

  .about-container {
    max-width: 1400px;
    margin: 0 auto;
    padding-top: 60px;
  }

  /* 标题区域 */
  .about-header {
    text-align: center;
    margin-bottom: 40px;
    padding: 0 20px;
  }

  .brand-slogan {
    color: #222;
    font-size: 26px;
    line-height: 1.6;
    margin-bottom: 12px;
    font-weight: 600;
  }

  /* 突出显示标语中的主题色 */
  .brand-slogan span {
    color: #f76b1b;
  }

  /* 主体内容区布局 */
  .about-content {
    display: flex;
    gap: 30px;
    height: calc(100vh - 220px);
    min-height: 400px;
  }

  /* 左侧目录 */
  .about-sidebar {
    width: 300px;
    flex-shrink: 0;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    overflow: hidden;
  }

  .toc {
    height: 100%;
    overflow-y: auto;
    padding: 10px 0;
  }

  .toc ul {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .toc-item {
    padding: 12px 20px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: flex-start;
    color: #333;
    font-size: 14px;
    border-left: 3px solid transparent;
    user-select: none;
    width: 100%;
    box-sizing: border-box;
  }

  .toc-item:hover {
    background-color: rgba(247, 107, 27, 0.05);
    padding-left: 22px;
  }

  .toc-item.active {
    background-color: rgba(247, 107, 27, 0.1);
    border-left-color: #f76b1b;
    color: #f76b1b;
    font-weight: 500;
  }

  .toc-number {
    font-weight: 600;
    margin-right: 8px;
    min-width: 20px;
  }

  .toc-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 右侧内容区 */
  .about-main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .sections-container {
    flex: 1;
    overflow-y: auto;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    padding: 35px 45px;
    line-height: 1.8;
  }

  .about-section {
    margin-bottom: 50px;
    padding-bottom: 30px;
    border-bottom: 1px solid #f0f0f0;
  }

  .about-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
  }

  .section-heading {
    color: #222;
    font-size: 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
  }

  .heading-number {
    color: #f76b1b;
    font-weight: 700;
    margin-right: 10px;
  }

  .section-body {
    color: #444;
    font-size: 15px;
  }

  .section-body ul {
    margin: 20px 0;
    padding-left: 24px;
  }

  .section-body li {
    margin-bottom: 12px;
    position: relative;
    padding-left: 8px;
  }

  .section-body strong {
    color: #f76b1b;
    font-weight: 600;
  }

  /* 响应式适配 */
  @media (max-width: 1024px) {
    .about-content {
      flex-direction: column;
      height: auto;
      gap: 20px;
    }

    .about-sidebar {
      width: 100%;
      max-height: 180px;
    }

    .sections-container {
      padding: 25px 30px;
      max-height: 500px;
    }

    .brand-slogan {
      font-size: 22px;
    }
  }

  @media (max-width: 768px) {
    .about-header {
      margin-bottom: 30px;
    }

    .brand-slogan {
      font-size: 20px;
    }

    .toc-item {
      padding: 10px 15px;
      font-size: 13px;
    }

    .section-heading {
      font-size: 18px;
    }

    .section-body {
      font-size: 14px;
    }
  }
</style>
